<?php
/**
 * vanephp web framework
 * 验证助手
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/s
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package helper
 */

if (! defined ( 'INVANE' ))
	exit ( '!' );

class vane_helper_validator {
	private $value = null;
	
	public $validateErrors = array(
		'notEmpty' => '字段不能为空',
		'min' => '数值必须大于等于%s',
		'max' => '数值必须小于等于%s',
		'minlength' => '最小长度为%s',
		'maxlength' => '最大长度为%s',
		'date' => '日期格式不正确',
		'time' => '时间格式不正确',
		'range' => '长度为%s到%s之间',
		'ip' => 'IP格式不正确',
		'url' => '网址格式不正确',
		'number' => '只能输入数值',
		'email' => 'email格式不正确',
		'username' => '用户名格式不正确',
		'qq' => 'qq号码格式不正确',
		'tel' => '电话号码不正确',
	);
	
	public function setValue($val) {
		$this->value = $val;
	}
	
	public function getError($fun) {
		return isset ( $this->validateErrors [$fun] ) ? $this->validateErrors [$fun] : null;
	}
	
	public function notEmpty() {
		$this->value = trim ( $this->value );
		return $this->value != '';
	}
	
	public function min($min) {
		$this->validateErrors [__FUNCTION__] = sprintf ( $this->validateErrors [__FUNCTION__], $min );
		return $this->value >= $min;
	}
	
	public function max($max) {
		$this->validateErrors [__FUNCTION__] = sprintf ( $this->validateErrors [__FUNCTION__], $max );
		return $this->value <= $max;
	}
	
	public function minlength($min) {
		$this->validateErrors [__FUNCTION__] = sprintf ( $this->validateErrors [__FUNCTION__], $min );
		return strlen ( $this->value ) >= $min;
	}
	
	public function maxlength($max) {
		$this->validateErrors [__FUNCTION__] = sprintf ( $this->validateErrors [__FUNCTION__], $max );
		return strlen ( $this->value ) <= $max;
	}
	
	public function range($min, $max) {
		$this->validateErrors [__FUNCTION__] = sprintf ( $this->validateErrors [__FUNCTION__], $min, $max );
		return $this->min ( $min ) && $this->max ( $max );
	}
	
	public function number() {
		return is_numeric ( $this->value );
	}
	
	public function date() {
		return $this->match ( '/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/' );
	}
	
	public function time() {
		return $this->match ( '^\[0-2]{1}\[0-6]{1}:\[0-5]{1}\[0-9]{1}:\[0-5]{1}\[0-9]{1}' );
	}
	
	public function ip() {
		return ((false === ip2long ( $this->value )) || (long2ip ( ip2long ( $this->value ) ) !== $this->value)) ? false : true;
	}
	
	public function email() {
		return $this->match ( '/^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/' );
	}
	
	public function url() {
		return $this->match ( '/^((https?|ftp|news):\/\/)?([a-z]([a-z0-9\-]*\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$/' );
	}
	
	public function username() {
		return $this->match ( '/^[a-zA-Z][a-zA-Z0-9_]{3,16}$/' );
	}
	
	public function chineseid() {
		return $this->match ( '/\d{15}|\d{18}/' );
	}
	
	public function qq() {
		return $this->range ( 10000, 99999999999 );
	}
	
	public function tel() {
		return $this->match ( '/^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/' );
	}
	
	public function chinese() {
		return $this->match ( '/^[\u4e00-\u9fa5],{0,}$/' );
	}
	
	private function match($pattern) {
		return ( bool ) preg_match ( $pattern, $this->value );
	}
}